package com.carbohydrate42.kotlin_compose_demo.compose

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.material3.Button
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import java.util.Date
import java.util.Timer
import kotlin.concurrent.fixedRateTimer

/**
 * 参考 (有点小问题)
 * https://juejin.cn/post/6969143243367776287
 */
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun TryInnerAlertAndNotification() {

    var timer: Timer? = null

    var count by remember {
        mutableStateOf(0)
    }

    var shown by remember {
        mutableStateOf(false)
    }


    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(
            onClick = { shown = !shown }) {
            Text(text = "$shown")
        }

        // todo 倒计时&计数好像不是这么写的, 这样搞都没法停止计时, 应该有好的办法
        Row(horizontalArrangement = Arrangement.SpaceAround) {
            Button(onClick = {
                timer = fixedRateTimer("timer1", false, Date(), 1000) {
                    count--
                }
            }) {
                Text(text = "start timer1")
            }

            Button(onClick = {
                timer?.cancel()
            }) {
                Text(text = "end timer1")
            }
            
            Text(text = "$count")
        }
        
    }

    AnimatedVisibility(
        visible = shown,
        enter = slideInVertically(
            // Enters by sliding down from offset -fullHeight to 0.
            initialOffsetY = { fullHeight -> -fullHeight }
        ),
        exit = slideOutVertically(
            // Exits by sliding up from offset 0 to -fullHeight.
            targetOffsetY = { fullHeight -> -fullHeight }
        )
    ) {
        Surface(
            modifier = Modifier.fillMaxWidth(),
            color = Color.Green,
        ) {
            Text(
                text = "something",
                modifier = Modifier.padding(16.dp)
            )
        }
    }
}